package com.dfdchem.imd.kaiwu.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class VersionUtil {

    public static String getAppVersionName(Context context) {
        String versionName = "";
        int versionCode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            versionCode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionUtil", "getAppVersionName Exception", e);
        }
        return versionName;
    }

    public static int getAppVersionCode(Context context) {
        String versionName = "";
        int versionCode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            versionCode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return 0;
            }
        } catch (Exception e) {
            Log.e("VersionUtil", "getAppVersionCode", e);
        }
        return versionCode;
    }

    public static String getPackageName(Context context) {
        String packageName = "";
        try {
            // ---get the package info---
            packageName = context.getPackageName();

            if (packageName != null) {
                return packageName;
            }
        } catch (Exception e) {
            Log.e("VersionUtil", "getPackageNameException", e);
        }
        return packageName;
    }
}